/*
 * @Author: 张小平
 * @Date:   2025-05-01
 * @Email:  qqshuqian@163.com
 * @Copyright © 2024 aseyun. All rights reserved.
 */

import {defineConfig, loadEnv} from 'vite'
import createVitePlugins from './vite/plugins'
import {visualizer} from 'rollup-plugin-visualizer'

const { resolve } = require('path')


// https://vitejs.dev/config/
export default ({ mode, command }) => {
  const env = loadEnv(mode, process.cwd())
  const { VITE_APP_BASE } = env
  return defineConfig({
    base: VITE_APP_BASE,

    resolve: {
      alias: {
        '~': resolve(__dirname, './'),
        "@": resolve(__dirname, "./src"),
        vue: 'vue/dist/vue.esm-bundler.js',
        "@framework-core": resolve(__dirname, "./src/framework-core"),
        "@framework-code": resolve(__dirname, "./src/framework-code"),
        "@framework-admin": resolve(__dirname, "./src/framework-admin"),
        "components": resolve(__dirname, "./src/components"),
        "styles": resolve(__dirname, "./src/styles"),
        "utils": resolve(__dirname, "./src/utils"),
      }
    },
    plugins: createVitePlugins(env, command === 'build'),
    build:{
      minify: 'terser',
      //   关闭文件计算
      reportCompressedSize: false,
      sourcemap: false, // 这个生产环境一定要关闭，不然打包的产物会很大
      terserOptions: {
        // 清除console和debugger
        compress: {
          drop_console: true,
          drop_debugger: true,
        },
      },
      rollupOptions:{
        // 确保外部化处理那些你不想打包进库的依赖
        external: ['vue', 'element-plus','monaco-editor'],
        output: {
          // 静态资源打包做处理
          chunkFileNames: 'assets/js/[name]-[hash].js',
          entryFileNames: 'assets/js/[name]-[hash].js',
          assetFileNames: 'assets/[ext]/[name]-[hash].[ext]',
          manualChunks(id) {
            if (id.includes('node_modules')) {
              return id.toString().split('node_modules/')[1].split('/')[0].toString();
            }
          },
        },
        plugins:[visualizer()]
      }
    }

  })
}
